package cn.cdeden.sys.utils.domain;

import cn.cdeden.sys.domain.dto.SysParametersConfigDto;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
//import com.cdyd.ecm.sys.domain.dto.SysParametersConfigDto;
//import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @ProjectName : ecm_svn
 * @Package : com.cdyd.ecm.sys.utils.domain
 * @ClassName : ApConfig
 * @Author : Yu.
 * @Description : // ad域配置
 * @Date : 2024/4/23 13:26
 * @Version : 1.0
 */
@Data
@Builder
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class OAuthConfig implements Serializable {
    private static final long serialVersionUID = 1L;

//    @ApiModelProperty(value = "是否开启状态")
    private boolean status;

//    @ApiModelProperty(value = "跳转地址")
    private String redirectUri;

//    @ApiModelProperty(value = "密钥")
    private String clientSecret;

//    @ApiModelProperty(value = "认证方式")
    private String grantType;

//    @ApiModelProperty(value = "OAuth认证地址")
    private String url;

//    @ApiModelProperty(value = "应用标识")
    private String clientId;



    public OAuthConfig(List<SysParametersConfigDto> infoList) {
        Map<String, SysParametersConfigDto> collect = infoList
                .stream()
                .collect(Collectors.toMap(SysParametersConfigDto::getConfigCode,
                        Function.identity()));
        SysParametersConfigDto status = collect.get("OAuth_enable");
        SysParametersConfigDto redirectUri = collect.get("OAuth_redirectUri");
        SysParametersConfigDto clientSecret = collect.get("OAuth_clientSecret");
        SysParametersConfigDto grantType = collect.get("OAuth_grantType");
        SysParametersConfigDto url = collect.get("OAuth_url");
        SysParametersConfigDto clientId = collect.get("OAuth_clientId");

        this.status = Boolean.parseBoolean(status.getConfigValue());
        this.redirectUri = redirectUri.getConfigValue();
        this.clientSecret = clientSecret.getConfigValue();
        this.grantType = grantType.getConfigValue();
        this.url = url.getConfigValue();
        this.clientId = clientId.getConfigValue();
    }



    public void copyFrom(OAuthConfig source){
        BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
    }






    /**
     * 获取必填字段
     * @return
     */
    public String getMandatory() {
        String info01 = "系统设置中集成配置,OAuth配置 `";
        StringJoiner stringJoiner = new StringJoiner(",");
        String info02 = "`信息不能为空";

        if (StringUtils.isBlank(this.redirectUri)){
            stringJoiner.add("OAuth_redirectUri");
        }
        if (StringUtils.isBlank(this.clientSecret)){
            stringJoiner.add("OAuth_clientSecret");
        }
        if (StringUtils.isBlank(this.grantType)){
            stringJoiner.add("OAuth_grantType");
        }
        if (StringUtils.isBlank(this.url)){
            stringJoiner.add("OAuth_url");
        }
        if (StringUtils.isBlank(this.clientId)){
            stringJoiner.add("OAuth_clientId");
        }



        int length = stringJoiner.length();

        // 判断长度
        return length > 0 ? info01 + stringJoiner + info02 : null;
    }


}
